'use strict';

/**
 * @param {Egg.Application} app - egg application
 */
module.exports = app => {
  const apiV1Router = app.router.namespace('/api/v1');
  const {controller, middleware} = app;

  const {userApi, messageApi, topicApi, replyApi, collectApi, suggApi, searchApi} = controller.api;

  const cors = middleware.cors();
  const tokenRequired = middleware.tokenRequired();
  const pagination = middleware.pagination();
  // const createTopicLimit = middleware.createTopicLimit(config.topic);
  // const createUserLimit = middleware.createUserLimit(config.create_user_per_ip);

  // 用户
  apiV1Router.get('/user/:loginname', userApi.show);
  apiV1Router.post('/access_token', tokenRequired, userApi.verify);
  apiV1Router.post('/login', userApi.login);
  apiV1Router.options('/login4Chat', cors);  // 处理 OPTIONS 请求
  apiV1Router.post('/login4Chat', cors, userApi.login4Chat);

  // 消息通知
  apiV1Router.get('/message/count', tokenRequired, messageApi.count);
  apiV1Router.get('/messages', tokenRequired, messageApi.list);
  apiV1Router.post('/message/mark_all', tokenRequired, messageApi.markAll);
  apiV1Router.post('/message/mark_one/:msg_id', tokenRequired, messageApi.markOne);

  // 主题
  apiV1Router.get('/topics', pagination, topicApi.index);
  apiV1Router.get('/topic/:id', topicApi.show);
  // apiV1Router.post('/topics', tokenRequired, topicApi.create);
  // apiV1Router.post('/topics/update', tokenRequired, topicApi.update);
  apiV1Router.get('/noshow', topicApi.noshow);
  apiV1Router.post('/topic/listOriginalTags', tokenRequired, topicApi.listOriginalTags); // 展示用户所有原创标签及标签包含文章的数量, 可以按照标签名模糊搜索
  apiV1Router.post('/topic/updateOriginalTagName', tokenRequired, topicApi.updateOriginalTagName); // 修改原创标签名
  apiV1Router.post('/topic/removeOriginalTag', tokenRequired, topicApi.removeOriginalTag); // 删除原创标签


  // 收藏相关接口
  apiV1Router.post('/collect/share', tokenRequired, collectApi.share); // 通过插件收藏
  apiV1Router.post('/collect/cancel_share', tokenRequired, collectApi.cancel_share); // 通过插件取消收藏
  apiV1Router.post('/collect/listTags', tokenRequired, collectApi.listTags); // 展示用户所有收藏标签及标签包含文章的数量, 可以按照标签名模糊搜索
  apiV1Router.post('/collect/showInfo', tokenRequired, collectApi.showInfo); // 展示用户所有收藏标签及标签包含文章的数量+该链接是否已收藏


  // 评论
  apiV1Router.post('/topic/:topic_id/replies', tokenRequired, replyApi.create);
  apiV1Router.post('/reply/:reply_id/ups', tokenRequired, replyApi.updateUps);


  apiV1Router.post('/sugg', suggApi.sugg);


  apiV1Router.post('/searchApiBaidu', searchApi.searchApiBaidu);
  apiV1Router.post('/searchApiTsou', searchApi.searchApiTsou);

};
